// src/router/index.js

import { createRouter, createWebHistory } from 'vue-router';
import Login from '@/components/Login.vue';
import Register from '@/components/Register.vue'; // 如果你有注册页
import Guide from '@/components/guide.vue';
import User from '@/components/user.vue';
import MyReviews from '@/components/MyReviews.vue';
import MovieDetails from '@/components/moviedetails.vue';
import MyWatchlist from '@/components/MyWatchlist.vue';
import AnnualMovie from '@/view/annualMovie/index.vue';
import MoreRank from '@/view/moreRank/index.vue';
import { useAuthStore } from '@/store/authStore';
import SearchMovie from '@/view/search/index.vue';
import UserProfile from '@/view/userProfile/index.vue';


const routes = [
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/home',
        name: 'Home',
        component: Guide,
        meta: { requiresAuth: true }
    },
    {
        path: '/profile',
        name: 'Profile',
        component: User,
        meta: { requiresAuth: true }
    },
    {
        path: '/my-reviews',
        name: 'MyReviews',
        component: MyReviews,
        meta: { requiresAuth: true }
    },
    {
        path: '/my-watchlist',
        name: 'MyWatchlist',
        component: MyWatchlist,
        meta: { requiresAuth: true }
    },
    {
        path: '/movie/:id',
        name: 'MovieDetails',
        component: MovieDetails,
        meta: { requiresAuth: true }
    },
    {
        path: '/annual',
        name: 'AnnualMovie',
        component: AnnualMovie,
        meta: { requiresAuth: true }
    },
    {
        path: '/more-rank/:name',
        name: 'MoreRank',
        component: MoreRank,
        meta: { requiresAuth: true }
    },
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/searchMovie',
        name: 'SearchMovie',
        component: SearchMovie,
        meta: { requiresAuth: true }
    },
    {
        path: '/user/:id',
        name: 'UserProfile',
        component: UserProfile,
        meta: { requiresAuth: true }
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    // 获取认证状态 - 使用更安全的方式
    let isAuthenticated = false;
    try {
        const authStore = useAuthStore();
        isAuthenticated = authStore.isAuthenticated;
    } catch (error) {
        // 如果Pinia还没初始化，从localStorage直接读取
        const savedAuth = localStorage.getItem('auth');
        if (savedAuth) {
            const { isAuthenticated: savedAuthState } = JSON.parse(savedAuth);
            isAuthenticated = savedAuthState;
        }
    }
    
    // 如果路由需要认证
    if (to.meta.requiresAuth) {
        // 检查是否已登录
        if (!isAuthenticated) {
            // 未登录，重定向到登录页
            next('/login');
            return;
        }
    }
    
    // 如果已登录用户访问登录页，重定向到首页
    if (to.path === '/login' && isAuthenticated) {
        next('/home');
        return;
    }
    
    // 其他情况正常跳转
    next();
});

export default router;
